// ConfigAdapter.cpp
#include "ImageProcessor.h"
#include "configadapter.h"
#include "configmanager.h"
#include "errordialogmanager.h"
#include "popupmanager.h"
#include "qtconcurrentrun.h"
#include <QDebug>
#include <fstream>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QDir>

ConfigAdapter::ConfigAdapter(QObject* parent)
    : QObject(parent) {

}

void ConfigAdapter::loadConfigTable(const QString& configTableFile) {
    std::lock_guard<std::mutex> lock(mutex_);
    LogInfo << "Loading configuration table: " << configTableFile.toStdString();
    try {
        ConfigManager::getInstance().loadConfigTable(configTableFile.toStdString());
        LogInfo << "Configuration table loaded successfully.";
    } catch (const std::exception& e) {
        LogError << "Failed to load configuration table: " << e.what();
    }
}

void ConfigAdapter::loadOrCreateValueTable(const QString& valueTableFile) {
    QtConcurrent::run([this,valueTableFile]() {
        std::lock_guard<std::mutex> lock(mutex_);
        ImageProcessor::getInstance().setcaptureImgMode(ImageProcessor::CapPause);
        PopupManager::instance().showPopup("加载配置项", "正在加载配置中…………");
        QThread::sleep(2);
        LogInfo << "Loading or creating value table: " << valueTableFile.toStdString();

        try {
            ConfigManager::getInstance().loadOrCreateValueTable(valueTableFile.toStdString());
            PopupManager::instance().hidePopup();
            LogInfo << "Value table loaded or created successfully.";
        } catch (const std::exception& e) {
            ErrorDialogManager::instance().showNonBlockingInfo("配置导入完成","导入失败从");
            LogError << "Failed to load or create value table: " << e.what();
        }
        //ImageProcessor::getInstance().setcaptureImgMode(ImageProcessor::CapPause);
        ErrorDialogManager::instance().showNonBlockingInfo("配置导入完成","导入成功");
    });
}

void ConfigAdapter::saveValueTable(const QString& valueTableFile) {

    std::lock_guard<std::mutex> lock(mutex_);
    LogInfo << "Saving value table: " << valueTableFile.toStdString();
    try {
        ConfigManager::getInstance().saveValueTable(valueTableFile.toStdString());
        LogInfo << "Value table saved successfully.";
    } catch (const std::exception& e) {
        LogError << "Failed to save value table: " << e.what();
    }
}

QVariantMap ConfigAdapter::getAllParameters() const {
    std::lock_guard<std::mutex> lock(mutex_);
    LogInfo << "Retrieving all parameters.";
    try {
        return ConfigManager::getInstance().getAllParameters();
    } catch (const std::exception& e) {
        LogError << "Failed to retrieve all parameters: " << e.what();
        return QVariantMap();
    }
}

QVariantMap ConfigAdapter::getSectionParameters(const QString& section) const {
    std::lock_guard<std::mutex> lock(mutex_);
    if (section.isEmpty())
    {
        LogWarning<<"Retrieving parameters for section is empty";
        return QVariantMap();
    }
    LogInfo << "Retrieving parameters for section: " << section.toStdString();
    try {
        QVariantMap sectionMap;
        QVariantMap allParameters = ConfigManager::getInstance().getAllParameters();
        if (allParameters.contains(section)) {
            sectionMap = allParameters[section].toMap();
            LogInfo << "Successfully retrieved parameters " << sectionMap;
        } else {
            LogWarning << "Section not found: " << section.toStdString();
        }
        return sectionMap;
    } catch (const std::exception& e) {
        LogError << "Failed to retrieve parameters for section: " << section.toStdString() << " - " << e.what();
        return QVariantMap();
    }
}

QVariant ConfigAdapter::getParameter(const QString& section, const QString& key) const {
    std::lock_guard<std::mutex> lock(mutex_);
    LogInfo << "Retrieving parameter [" << section.toStdString() << "] - " << key.toStdString();
    try {
        QVariant value = ParamValue.getParameter(section, key);
        if (value.isValid()) {
            LogInfo << "Successfully retrieved parameter [" << section.toStdString() << "] - " << key.toStdString();
        } else {
            LogWarning << "Parameter not found [" << section.toStdString() << "] - " << key.toStdString();
        }
        return value;
    } catch (const std::exception& e) {
        LogError << "Failed to retrieve parameter [" << section.toStdString() << "] - " << key.toStdString() << ": " << e.what();
        return QVariant();
    }
}

QVariantMap ConfigAdapter::getConfig(const QString& section, const QString& key) const
{
    std::lock_guard<std::mutex> lock(mutex_);
    LogDebug<< "Retrieving getConfig [" << section.toStdString() << "] - " << key.toStdString();
    try {
        QVariantMap value = ConfigManager::getInstance().getConfig(section.toStdString(), key.toStdString());
        if (!value.isEmpty()) {
            LogInfo << "Successfully retrieved config [" << section.toStdString() << "] - " << key.toStdString();
        } else {
            LogWarning << "Config not found [" << section.toStdString() << "] - " << key.toStdString();
        }
        return value;
    } catch (const std::exception& e) {
        LogError << "Failed to retrieve config [" << section.toStdString() << "] - " << key.toStdString() << ": " << e.what();
        return QVariantMap();
    }
}

void ConfigAdapter::setParameter(const QString& section, const QString& key, const QVariant& value) {
    std::lock_guard<std::mutex> lock(mutex_);
    LogInfo << "Setting parameter [" << section.toStdString() << "] - " << key.toStdString() << " to value " << value.toString().toStdString();
    try {
        ConfigManager::getInstance().setParameter(section,
                                                  key,
                                                  value);
        //LogInfo << "Successfully set parameter [" << section.toStdString() << "] - " << key.toStdString();
    } catch (const std::exception& e) {
        LogError << "Failed to set parameter [" << section.toStdString() << "] - " << key.toStdString() << ": " << e.what();
    }
}

